<?php

class Zy_fetchlist extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->library('uri');
		$this->load->database();
	}

	function index()
	{

	}

	function strToHex($string)   
	{   
		$hex = "";
		   
		for ($i = 0; $i < strlen($string); $i++)   
			$hex .= dechex(ord($string[$i]));
			   
		$hex = strtoupper($hex);
		   
		return $hex;   
	}
	
	function namelist()
	{
		$name = $this->input->get('name');
		$name = Myphplib::trim_space($name);

		$prescribetable = $this->load->get_var('prescribetable');

		if (!strcmp($name, "*"))
		{
			$querystring = "select name from $prescribetable order by convert(name using gb2312)";
		}
		else
		{
			$name = $this->db->escape_like_str($name);
			$querystring = "select name from $prescribetable where name like '%$name%' order by convert(name using gb2312)";
		}

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "source:[]";
			return;
		}

		echo "source:[";

		foreach($query->result() as $row)
		{
			echo "'".$row->name."',";
		}

		echo "]";
	}

	function patientlist()
	{
		$name = $this->input->get('name');
		$name = Myphplib::trim_space($name);

		$dbtable = $this->load->get_var('dbtable');

		if (!strcmp($name, "*"))
		{
			$querystring = "select name from $dbtable order by convert(name using gb2312)";
		}
		else
		{
			$name = $this->db->escape_like_str($name);
			$querystring = "select name from $dbtable where name like '%$name%' order by convert(name using gb2312)";
		}

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "source:[]";
			return;
		}

		$num = 0;
		$arr = array();

		foreach($query->result() as $row)
		{
			if (in_array($row->name, $arr))
			{
				continue;	
			}
			
			$arr[$num++] = $row->name;
		}
		
		echo "source:[";

		for ($i = 0; $i < $num; $i++)
		{
			echo "'".$arr[$i]."',";
		}

		echo "]";
	}

	function phonelist()
	{
		$phonenum = $this->input->get('phonenum');
		$phonenum = Myphplib::trim_space($phonenum);

		$dbtable = $this->load->get_var('dbtable');

		if (!strcmp($phonenum, "*"))
		{
			$querystring = "select phonenum from $dbtable";
		}
		else
		{
			$phonenum = $this->db->escape_like_str($phonenum);
			$querystring = "select phonenum from $dbtable where phonenum like '%$phonenum%'";
		}

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "source:[]";
			return;
		}

		$num = 0;
		$arr = array();

		foreach($query->result() as $row)
		{
			if (in_array($row->phonenum, $arr))
			{
				continue;	
			}
			
			$arr[$num++] = $row->phonenum;
		}
		
		echo "source:[";

		for ($i = 0; $i < $num; $i++)
		{
			echo "'".$arr[$i]."',";
		}

		echo "]";
	}
	
	function search()
	{
		$name = $this->input->get('prescribe');
		$field = "value";

		if (!$name || !strcmp($name, ""))
		{
			$name = $this->input->get('usemethod');
			$field = "usemethod";			
		}

		$prescribetable = $this->load->get_var('prescribetable');

		$name = $this->db->escape_like_str($name);
		
		$querystring = "select $field from $prescribetable where $prescribetable.name='$name'";

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}

		foreach($query->result() as $row)
		{
			echo $row->$field;
			break;
		}
	}
}